/* Sets the label from the dlState
 * Doesn't have to be a label,
 * sets label, accesskey and disabled properties
 */
function setLabels(dlState, pauseLabel, cancelLabel, launchLabel, strings)
{
	if(launchLabel)
	{
		launchLabel.setAttribute("label", strings.getString("launchAction"));
		launchLabel.setAttribute("accesskey", strings.getString("launchAccess"));
		
		if(dlState == Components.interfaces.nsIDownloadManager.DOWNLOAD_FINISHED)
			launchLabel.setAttribute("disabled", "false");
		else
			launchLabel.setAttribute("disabled", "true");
	}
	switch(dlState)
	{
		/* pause and cancel, both disabled */
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_NOTSTARTED:
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_SCANNING:
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_BLOCKED:
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_QUEUED:
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_FINISHED:
			pauseLabel.setAttribute("label", strings.getString("pauseAction"));
			pauseLabel.setAttribute("accesskey", strings.getString("pauseAccess"));
			pauseLabel.setAttribute("disabled", "true");
			
			cancelLabel.setAttribute("label", strings.getString("cancelAction"));
			cancelLabel.setAttribute("accesskey", strings.getString("cancelAccess"));
			cancelLabel.setAttribute("disabled", "true");
			break;
		
		/* pause and cancel, both enabled */
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_DOWNLOADING:
			pauseLabel.setAttribute("label", strings.getString("pauseAction"));
			pauseLabel.setAttribute("accesskey", strings.getString("pauseAccess"));
			pauseLabel.setAttribute("disabled", "false");
			
			cancelLabel.setAttribute("label", strings.getString("cancelAction"));
			cancelLabel.setAttribute("accesskey", strings.getString("cancelAccess"));
			cancelLabel.setAttribute("disabled", "false");
			break;
		
		/* resume and cancel, both enabled */
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_PAUSED:
			pauseLabel.setAttribute("label", strings.getString("resumeAction"));
			pauseLabel.setAttribute("accesskey", strings.getString("resumeAccess"));
			pauseLabel.setAttribute("disabled", "false");
			
			cancelLabel.setAttribute("label", strings.getString("cancelAction"));
			cancelLabel.setAttribute("accesskey", strings.getString("cancelAccess"));
			cancelLabel.setAttribute("disabled", "false");
			break;
		
		/* pause disabled, retry enabled */
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_FAILED:
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_CANCELED:
			pauseLabel.setAttribute("label", strings.getString("pauseAction"));
			pauseLabel.setAttribute("accesskey", strings.getString("pauseAccess"));
			pauseLabel.setAttribute("disabled", "true");
			
			cancelLabel.setAttribute("label", strings.getString("retryAction"));
			cancelLabel.setAttribute("accesskey", strings.getString("retryAccess"));
			cancelLabel.setAttribute("disabled", "false");
			break;
	};
}

function disableLabels(pauseLabel, cancelLabel, launchLabel, strings)
{
	if(launchLabel)
	{
		launchLabel.setAttribute("label", strings.getString("launchAction"));
		launchLabel.setAttribute("accesskey", strings.getString("launchAccess"));
		launchLabel.setAttribute("disabled", "true");
	}
	pauseLabel.setAttribute("label", strings.getString("pauseAction"));
	pauseLabel.setAttribute("accesskey", strings.getString("pauseAccess"));
	pauseLabel.setAttribute("disabled", "true");
	
	cancelLabel.setAttribute("label", strings.getString("cancelAction"));
	cancelLabel.setAttribute("accesskey", strings.getString("cancelAccess"));
	cancelLabel.setAttribute("disabled", "true");
}

function setImages(dlState, pauseImage, pauseImageState, cancelImage, cancelImageState, strings)
{
	switch(dlState)
	{
		/* pause and cancel, both disabled */
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_NOTSTARTED:
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_SCANNING:
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_BLOCKED:
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_QUEUED:
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_FINISHED:
			pauseImage.setAttribute("src", "chrome://downloadtabs/skin/pause_off_disabled.png");
			pauseImage.removeAttribute("tooltiptext");
			
			cancelImage.setAttribute("src", "chrome://downloadtabs/skin/cancel_off_disabled.png");
			cancelImage.removeAttribute("tooltiptext");
			break;
		
		/* pause and cancel, both enabled */
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_DOWNLOADING:
			if(pauseImageState)
				pauseImage.setAttribute("src", "chrome://downloadtabs/skin/pause_on.png");
			else
				pauseImage.setAttribute("src", "chrome://downloadtabs/skin/pause_off.png");
			pauseImage.setAttribute("tooltiptext", strings.getString("pauseAction"));
			
			if(cancelImageState)
				cancelImage.setAttribute("src", "chrome://downloadtabs/skin/cancel_on.png");
			else
				cancelImage.setAttribute("src", "chrome://downloadtabs/skin/cancel_off.png");
			cancelImage.setAttribute("tooltiptext", strings.getString("cancelAction"));
			break;
		
		/* resume and cancel, both enabled */
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_PAUSED:
			if(pauseImageState)
				pauseImage.setAttribute("src", "chrome://downloadtabs/skin/resume_on.png");
			else
				pauseImage.setAttribute("src", "chrome://downloadtabs/skin/resume_off.png");
			pauseImage.setAttribute("tooltiptext", strings.getString("resumeAction"));
			
			if(cancelImageState)
				cancelImage.setAttribute("src", "chrome://downloadtabs/skin/cancel_on.png");
			else
				cancelImage.setAttribute("src", "chrome://downloadtabs/skin/cancel_off.png");
			cancelImage.setAttribute("tooltiptext", strings.getString("cancelAction"));
			break;
		
		/* pause disabled, retry enabled */
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_FAILED:
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_CANCELED:
			pauseImage.setAttribute("src", "chrome://downloadtabs/skin/pause_off_disabled.png");
			pauseImage.removeAttribute("tooltiptext");
			
			if(cancelImageState)
				cancelImage.setAttribute("src", "chrome://downloadtabs/skin/retry_on.png");
			else
				cancelImage.setAttribute("src", "chrome://downloadtabs/skin/retry_off.png");
			cancelImage.setAttribute("tooltiptext", strings.getString("retryAction"));
			break;
	};
}

function disableImages(pauseImage, pauseImageState, cancelImage, cancelImageState, strings)
{
	pauseImage.setAttribute("src", "chrome://downloadtabs/skin/pause_off_disabled.png");
	pauseImage.removeAttribute("tooltiptext");
	
	cancelImage.setAttribute("src", "chrome://downloadtabs/skin/cancel_off_disabled.png");
	cancelImage.removeAttribute("tooltiptext");
}

function pauseDownload(download, dm)
{
	if(download.state == Components.interfaces.nsIDownloadManager.DOWNLOAD_DOWNLOADING)
		dm.pauseDownload(download.id);
	else if(download.state == Components.interfaces.nsIDownloadManager.DOWNLOAD_PAUSED)
		dm.resumeDownload(download.id);
}

function cancelDownload(download, dm)
{
	switch(download.state)
	{
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_DOWNLOADING:
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_PAUSED:
			dm.cancelDownload(download.id);
			break;
		
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_FAILED:
		case Components.interfaces.nsIDownloadManager.DOWNLOAD_CANCELED:
			dm.retryDownload(download.id);
			break;
	};
}

function removeDownload(download, dm)
{
	dm.removeDownload(download.id);
}

function deleteDownload(download, dm)
{
	var file = (download.targetFile || download.target);
	removeDownload(download, dm);
	if(file.exists())
		file.remove(true);
}

function copyDownload(download)
{
	Components.classes["@mozilla.org/widget/clipboardhelper;1"].
		getService(Components.interfaces.nsIClipboardHelper).copyString(download.source.prePath + download.source.path);
}

function openDownloadFolder(download)
{
	var file = (download.targetFile || download.target);
	var folder = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
	folder.initWithPath(file.parent.path);
	folder.launch();
}

function launch(download)
{
	var file = (download.targetFile || download.target);
	file.launch();
}